package com.groot.medical.servlet;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;
import com.groot.medical.entity.Menu;
import com.groot.medical.entity.PageBean;
import com.groot.medical.entity.PersonalFiles;
import com.groot.medical.service.PayService;
import com.groot.medical.service.PayServiceImpl;
import com.groot.medical.service.PersonalFileService;
import com.groot.medical.service.PersonalFileServiceImpl;
@WebServlet("/sys/person.do")
public class PersonalFileController extends BaseServletController{
	private PersonalFileService personalFileService = new PersonalFileServiceImpl();
	
	private static final long serialVersionUID = 1L;
	
	private PayService payService = new PayServiceImpl();
	private static Logger LOGGER = Logger.getLogger(LoginController.class);
	
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}
	/**
	 * 分页查询参合农民档案信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findPersonByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<Menu> menus=(List<Menu>) request.getSession().getAttribute("menuList");
		List<Menu> result = new ArrayList<Menu>();
		/*根据页面传过来的id与该用户所有权限的上一级id比较
		 * 若该权限的上一级传过来的id相同则存入新集合
		 */
		for(Menu menu: menus){
			if(menu.getMenuPid().equals(tgId)){
				result.add(menu);
			}
		}
		Gson gson = new Gson();
		request.setAttribute("resultMenus", gson.toJson(result));
		request.setAttribute("tgId", tgId);
		int pageSize = 5;
		PageBean areaPage = personalFileService.findPersonByPage(pageNum, pageSize);
		System.out.println("areaPage:"+areaPage);
		request.setAttribute("areaPage", areaPage);
		return "/WEB-INF/page/fram/person_list.jsp";
	}
	/**
	 * 打开添加参合农民档案信息页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openAddPerson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("tgId",request.getParameter("tgId"));
		request.setAttribute("familyCode",request.getParameter("familyCode"));
		return "/WEB-INF/page/fram/person_add.jsp";
		
		
		
	}
	/**
	 * 添加参合农民档案信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addPersonInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				String tgId = request.getParameter("tgId");
				System.out.println("嘿嘿额黑恶虎"+tgId);
				String pageNum = request.getParameter("pageNum");
				//参合农民档案信息
				
				String holderRelation = request.getParameter("holderRelation");
				String idcardNumber = request.getParameter("idcardNumber");
				String Sex = request.getParameter("Sex");
				String Birthday = request.getParameter("Birthday");
				String Nation = request.getParameter("Nation");
				String Health = request.getParameter("Health");
				String cultrueDegree = request.getParameter("cultrueDegree");
				String personProperty = request.getParameter("personProperty");
				String isContryside = request.getParameter("isContryside");
				String isMarry = request.getParameter("isMarry");
				String workAddress = request.getParameter("workAddress");
				String phoneNumber = request.getParameter("phoneNumber");
				
				String familyCode = request.getParameter("familyCode");
				/*String farmingcardCode = request.getParameter("farmingcardCode");*/
				String medicalcardCode = request.getParameter("medicalcardCode");
				String Name = request.getParameter("Name");
				String Occupation = request.getParameter("Occupation");
				String oftenAddress = request.getParameter("oftenAddress");
				
				Date date1 = Date.valueOf(Birthday);
				//封装进户主信息实体（参合农民档案）
				PersonalFiles person = new PersonalFiles();
				person.setName(Name);
				person.setFamilyCode(familyCode);
				
				person.setHolderRelation(holderRelation);
				person.setIdcardNumber(idcardNumber);
				person.setSex(Sex);
				person.setBirthday(date1);
				person.setNation(Nation);
				person.setHealth(Health);
				person.setCultrueDegree(cultrueDegree);
				person.setPersonProperty(personProperty);
				person.setIsContryside(isContryside);
				person.setIsMarry(isMarry);
				person.setWorkAddress(workAddress);
				person.setPhoneNumber(phoneNumber);
				
				
				/*person.setFarmingcardCode(farmingcardCode);*/
				person.setMedicalcardCode(medicalcardCode);
				person.setOccupation(Occupation);
				person.setOftenAddress(oftenAddress);
				
				personalFileService.addPersonInfo(person);
				request.setAttribute("tgId", tgId);
				return findPersonByPage(request,response);
	}
	/**
	 * 打开参合农民信息修改界面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openEditPerson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("tgId",request.getParameter("tgId"));
		String idcardNumber = request.getParameter("id");
		//查询参合农民个人信息页面
		PersonalFiles person = personalFileService.findPersonInfoById(idcardNumber);
		request.setAttribute("person",person);
		return "/WEB-INF/page/fram/person_edit.jsp";
		
	}
	/**
	 * 修改参合农民个人档案信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editPersonInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String pageNum = request.getParameter("pageNum");
		//参合农民档案信息
		
		String holderRelation = request.getParameter("holderRelation");
		String idcardNumber = request.getParameter("idcardNumber");
		String Sex = request.getParameter("Sex");
		String Birthday = request.getParameter("Birthday");
		String Nation = request.getParameter("Nation");
		String Health = request.getParameter("Health");
		String cultrueDegree = request.getParameter("cultrueDegree");
		String personProperty = request.getParameter("personProperty");
		String isContryside = request.getParameter("isContryside");
		String isMarry = request.getParameter("isMarry");
		String workAddress = request.getParameter("workAddress");
		String phoneNumber = request.getParameter("phoneNumber");
		
		String familyCode = request.getParameter("familyCode");
		String farmingcardCode = request.getParameter("farmingcardCode");
		String medicalcardCode = request.getParameter("medicalcardCode");
		String Name = request.getParameter("Name");
		String Occupation = request.getParameter("Occupation");
		String oftenAddress = request.getParameter("oftenAddress");
		String familyincode = request.getParameter("familyInCode");
		
		Date date1 = Date.valueOf(Birthday);
		//封装进户主信息实体（参合农民档案）
		PersonalFiles person = new PersonalFiles();
		person.setName(Name);
		person.setFamilyCode(familyCode);
		person.setFamilyInCode(familyincode);
		person.setHolderRelation(holderRelation);
		person.setIdcardNumber(idcardNumber);
		person.setSex(Sex);
		person.setBirthday(date1);
		person.setNation(Nation);
		person.setHealth(Health);
		person.setCultrueDegree(cultrueDegree);
		person.setPersonProperty(personProperty);
		person.setIsContryside(isContryside);
		person.setIsMarry(isMarry);
		person.setWorkAddress(workAddress);
		person.setPhoneNumber(phoneNumber);
		
		
		person.setFarmingcardCode(farmingcardCode);
		person.setMedicalcardCode(medicalcardCode);
		person.setOccupation(Occupation);
		person.setOftenAddress(oftenAddress);
		personalFileService.editPersonInfo(person);
		request.setAttribute("tgId",130);
		
		return findPersonByPage(request,response);
	}
	
	public void deletePersonInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idCardNum = request.getParameter("idCardNum");
		String familyCode = request.getParameter("familyCode");
		String resultString = "false";
		//判断是否产生参合缴费记录
		int i = payService.findIsExsitPayService(idCardNum);
		if(i>0){
			//产生了记录
			resultString = "false";
			response.setContentType("text/html");
	        ServletOutputStream outputStream = response.getOutputStream();
	        outputStream.write(resultString.getBytes());
	        outputStream.close();
		}else{
			personalFileService.deletePersonInfo(idCardNum,familyCode);
			resultString = "true";
			response.setContentType("text/html");
	        ServletOutputStream outputStream = response.getOutputStream();
	        outputStream.write(resultString.getBytes());
	        outputStream.close();
		}
		System.out.println(idCardNum);
	}
}
